<template>
  <div>
    <BasicTable @register="registerTable">
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts" setup name="vipcardCheckRecord">
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/Table';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import { useBaseTable } from '@apps/antd-admin/src/hooks';

import { columns, searchFormSchema } from './data';
import { Api, cancelRecord } from './api';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';

const [registerDrawer, { openDrawer }] = useDrawer();
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'vipcardCheckRecord-template',
  tableProps: {
    //表格标题
    title: '核销记录',
    //主键字段
    rowKey: 'checkrecordId',
    //列表接口
    listUrl: Api.page,
    handlerName: 'busAdminVipCardCheckRecordListHandler',
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      showExportButton: true,
      exportButtonOptions: {
        code: 'ACTION_checkrecordExport',
      },
      schemas: searchFormSchema,
    },
    //操作列配置
    actionColumn: {
      width: 130,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
    //查询前处理
    beforeFetch: params => {
      params.useAirportIds = '';
      params.airportIds = '';
      let useAirportIdArray: any = params.useAirportIdArray;
      console.info('--------------beforeFetch-------useAirportIdArray--------------------------', useAirportIdArray);
      if (useAirportIdArray) {
        params.useAirportIds = useAirportIdArray.join(',');
      }
      delete params.useAirportIdArray;
      let airportIdArray: any = params.airportIdArray;
      console.info('--------------beforeFetch-------airportIdArray--------------------------', airportIdArray);
      if (airportIdArray) {
        params.airportIds = airportIdArray.join(',');
      }
      delete params.airportIdArray;
      let employeeIdArray: any = params.employeeIdArray;
      console.info('--------------beforeFetch-------employeeIdArray--------------------------', employeeIdArray);
      if (employeeIdArray) {
        params.employeeIds = employeeIdArray.join(',');
      }
      delete params.employeeIdArray;
      return params;
    },
  },
});

const [registerTable, { reload }, { rowSelection, hasSelectedRows }] = tableContext;
const { createMessage, createConfirm } = useMessage();

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '核验减录',
      onClick: handlerCancelRecord.bind(null, record),
      auth: 'ACTION_userGoodsAddCheckRecord',
    },
    {
      label: '删除',
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
      auth: 'ACTION_deleteCheckRecord',
    },
  ];
}

/**
 * 核验减录
 */
function handlerCancelRecord(record) {
  let checkrecordId = record.checkrecordId;
  createConfirm({
    iconType: 'info',
    title: '核验减录确认',
    content: '确认核验减录该条核验记录？',
    onOk: async () => {
      await cancelRecord({ checkrecordId: checkrecordId });
      handleSuccess(true, {});
    },
    onCancel: async () => {},
  });
}
</script>
